/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 25-ago-2011, 15:10:44
 */
package pro.unal.edu.sink;

import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
import pro.unal.edu.datasource.BMPImage;
import pro.unal.edu.datasource.JPEGImage;
import pro.unal.edu.datasource.TIFFImage;
import pro.unal.edu.sink.write.ImageWriter;

/**
 *
 * @author Carlos
 */
public class NewJFrame extends javax.swing.JFrame {

    private MyContainer container;
    private BMPImage bmp;
    private TIFFImage tif;
    private JPEGImage jpeg;
    private FileNameExtensionFilter filter = new FileNameExtensionFilter("BMP, TIFF, JPG, JPEG", "bmp", "tiff", "tif", "jpeg", "jpg");

    public JPanel getImagePanel() {
        return imagePanel;
    }

    public void setImagePanel(JPanel imagePanel) {
        this.imagePanel = imagePanel;
    }

    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        setExtendedState(MAXIMIZED_BOTH);
        container = new MyContainer();
        sliderAngle.setValue(0);
        container.setRotationAngle(0);
        fileChooser.setFileFilter(filter);
        sliderContrast.setValue(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        imagePanel = new javax.swing.JPanel();
        menuPanel = new javax.swing.JPanel();
        sliderAngle = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        angleL = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        invertCB = new javax.swing.JCheckBox();
        mirrorCB = new javax.swing.JCheckBox();
        grayCB = new javax.swing.JCheckBox();
        borderCB = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        sliderContrast = new javax.swing.JSlider();
        contrastL = new javax.swing.JLabel();
        myMenuBar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        openMenu = new javax.swing.JMenuItem();
        saveMenu = new javax.swing.JMenuItem();
        menuHelp = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(0, 0, 300, 300));
        setMinimumSize(new java.awt.Dimension(300, 300));

        imagePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Image"));
        imagePanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        imagePanel.setOpaque(false);
        imagePanel.setLayout(new java.awt.BorderLayout());
        getContentPane().add(imagePanel, java.awt.BorderLayout.CENTER);

        menuPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Menu"));
        menuPanel.setMinimumSize(new java.awt.Dimension(200, 229));
        menuPanel.setPreferredSize(new java.awt.Dimension(200, 229));

        sliderAngle.setMajorTickSpacing(360);
        sliderAngle.setMaximum(360);
        sliderAngle.setMinorTickSpacing(30);
        sliderAngle.setPaintLabels(true);
        sliderAngle.setPaintTicks(true);
        sliderAngle.setValue(5);
        sliderAngle.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderAngleStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Angle:");

        angleL.setText(" ");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Filters:");

        invertCB.setText("Invert Filter");
        invertCB.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        invertCB.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                invertCBItemStateChanged(evt);
            }
        });

        mirrorCB.setText("Mirror Filter");
        mirrorCB.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                mirrorCBItemStateChanged(evt);
            }
        });

        grayCB.setText("Gray Filter");
        grayCB.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                grayCBStateChanged(evt);
            }
        });
        grayCB.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                grayCBItemStateChanged(evt);
            }
        });

        borderCB.setText("Edge Filter");
        borderCB.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                borderCBItemStateChanged(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Contrast:");

        sliderContrast.setMajorTickSpacing(100);
        sliderContrast.setMinorTickSpacing(10);
        sliderContrast.setPaintLabels(true);
        sliderContrast.setPaintTicks(true);
        sliderContrast.setValue(0);
        sliderContrast.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderContrastStateChanged(evt);
            }
        });

        contrastL.setText(" ");

        javax.swing.GroupLayout menuPanelLayout = new javax.swing.GroupLayout(menuPanel);
        menuPanel.setLayout(menuPanelLayout);
        menuPanelLayout.setHorizontalGroup(
            menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menuPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(sliderContrast, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(sliderAngle, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addGroup(menuPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(angleL))
                    .addGroup(menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, menuPanelLayout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(contrastL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, menuPanelLayout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addGroup(menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(mirrorCB)
                                .addComponent(invertCB)
                                .addComponent(grayCB)
                                .addComponent(borderCB)))))
                .addContainerGap())
        );
        menuPanelLayout.setVerticalGroup(
            menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menuPanelLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(angleL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sliderAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(invertCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mirrorCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(grayCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(borderCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(contrastL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sliderContrast, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(79, 79, 79))
        );

        getContentPane().add(menuPanel, java.awt.BorderLayout.EAST);

        menuFile.setText("File");

        openMenu.setText("Open");
        openMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuActionPerformed(evt);
            }
        });
        menuFile.add(openMenu);

        saveMenu.setText("Save");
        saveMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveMenuActionPerformed(evt);
            }
        });
        menuFile.add(saveMenu);

        myMenuBar.add(menuFile);

        menuHelp.setText("Help");
        myMenuBar.add(menuHelp);

        setJMenuBar(myMenuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void openMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuActionPerformed
        String file = "";
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File fileP = fileChooser.getSelectedFile();
            file = fileP.getAbsolutePath();
            System.out.println(file.toString());
            String type = file.replace(".", "<").split("<")[1];
            if (type.equalsIgnoreCase("BMP")) {
                bmp = new BMPImage(file);
                container = new MyContainer(bmp, this);
            } else {
                if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG")) {
                    jpeg = new JPEGImage(file);
                    container = new MyContainer(jpeg, this);
                } else {
                    tif = new TIFFImage(file);
                    container = new MyContainer(tif, this);
                }
            }
            imagePanel.removeAll();
            imagePanel.add(container);
            int value = sliderAngle.getValue();
            container.setRotationAngle(2 * Math.PI * value / 360.d);
            imagePanel.setDoubleBuffered(true);
            setVisible(true);
        } else {
            System.out.println("File access cancelled by user.");
        }
    }//GEN-LAST:event_openMenuActionPerformed

    private void saveMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuActionPerformed
        System.out.println("Width: " + container.getImgd().getWidth() + "  --  Height: " + container.getImgd().getHeight());
        ImageWriter iw = new ImageWriter("copia.bmp", bmp);
    }//GEN-LAST:event_saveMenuActionPerformed

    private void sliderAngleStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderAngleStateChanged
        int value = sliderAngle.getValue();
        container.setRotationAngle(2 * Math.PI * value / 360.d);
        angleL.setText(sliderAngle.getValue() + "");

    }//GEN-LAST:event_sliderAngleStateChanged

private void sliderContrastStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderContrastStateChanged
    int value = sliderContrast.getValue();
    container.setContrast(90 * value / 100.d);
//    container.setContrast(360);
    System.out.println("Vontrast: " + value);
    contrastL.setText(sliderContrast.getValue() + "");
//    container.update(container.getGraphics());
    container.repaint();
}//GEN-LAST:event_sliderContrastStateChanged

private void grayCBStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_grayCBStateChanged
}//GEN-LAST:event_grayCBStateChanged

private void invertCBItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_invertCBItemStateChanged
    if (evt.getStateChange() == ItemEvent.SELECTED) {
        container.setIsInversionFilter(true);
    } else {
        container.setIsInversionFilter(false);
    }
}//GEN-LAST:event_invertCBItemStateChanged

private void mirrorCBItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_mirrorCBItemStateChanged
    if (evt.getStateChange() == ItemEvent.SELECTED) {
        container.setIsMirrorFilter(true);
    } else {
        container.setIsMirrorFilter(false);
    }
}//GEN-LAST:event_mirrorCBItemStateChanged

private void grayCBItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_grayCBItemStateChanged
    if (evt.getStateChange() == ItemEvent.SELECTED) {
        container.setIsGrayFilter(true);
    } else {
        container.setIsGrayFilter(false);
    }
}//GEN-LAST:event_grayCBItemStateChanged

private void borderCBItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderCBItemStateChanged
    if (evt.getStateChange() == ItemEvent.SELECTED) {
        container.setIsEdgeFilter(true);
    } else {
        container.setIsEdgeFilter(false);
    }
}//GEN-LAST:event_borderCBItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                NewJFrame n = new NewJFrame();
                n.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel angleL;
    private javax.swing.JCheckBox borderCB;
    private javax.swing.JLabel contrastL;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JCheckBox grayCB;
    private javax.swing.JPanel imagePanel;
    private javax.swing.JCheckBox invertCB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenu menuHelp;
    private javax.swing.JPanel menuPanel;
    private javax.swing.JCheckBox mirrorCB;
    private javax.swing.JMenuBar myMenuBar;
    private javax.swing.JMenuItem openMenu;
    private javax.swing.JMenuItem saveMenu;
    private javax.swing.JSlider sliderAngle;
    private javax.swing.JSlider sliderContrast;
    // End of variables declaration//GEN-END:variables

    public JCheckBox getBorderCB() {
        return borderCB;
    }

    public void setBorderCB(JCheckBox borderCB) {
        this.borderCB = borderCB;
    }

    public JCheckBox getGrayCB() {
        return grayCB;
    }

    public void setGrayCB(JCheckBox grayCB) {
        this.grayCB = grayCB;
    }

    public JCheckBox getInvertCB() {
        return invertCB;
    }

    public void setInvertCB(JCheckBox invertCB) {
        this.invertCB = invertCB;
    }

    public JCheckBox getMirrorCB() {
        return mirrorCB;
    }

    public void setMirrorCB(JCheckBox mirrorCB) {
        this.mirrorCB = mirrorCB;
    }
}
